package com.dudu.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.bean.ParamBean;
import com.dudu.enumeration.AbilityTypeEnum;
import com.dudu.service.IOperationPlatformService;
import com.dudu.utils.DictionaryUtils;
import com.dudu.utils.EnumUtility;
import com.dudu.utils.HttpRequest;

/**
 * 运营平台相关操作
 * 
 * @author 危东
 * @date 2016-4-20
 */
public class OperationPlatformServiceImpl implements IOperationPlatformService {
    // 日志输出
    private static final Logger log = Logger.getLogger(OperationPlatformServiceImpl.class.getSimpleName());
    private static String url = DictionaryUtils.getValueByTypeAndKey("platform", "url");

    // private String url="http://123.103.9.209:5002";
    // private String url ="http://test.cloud.yonyoutelecom.cn";
    /**
     * 6.2　客户信息接口 根据账号查询账号信息
     * 
     * @param cust_account
     * @return
     */
    public JSONObject queryCustomerInfo(String cust_account) {
	try {
	    JSONObject object = new JSONObject();
	    object.put("cust_account", cust_account);
	    String Myurl = url + "/CombinationServer/queryCustomerInfo.do";
	    String data = HttpRequest.postData(Myurl, object.toString());
	    log.info("OperationPlatformServiceImpl.queryCustomerInfo url=" + Myurl);
	    log.info("OperationPlatformServiceImpl.queryCustomerInfo req=" + object.toString());
	    log.info("OperationPlatformServiceImpl.queryCustomerInfo respone=" + data);
	    JSONObject response = JSONObject.fromObject(data);
	    String flag = response.getString("flag");
	    String desc = response.getString("desc");
	    if (flag.equals("0000")) {
		return response.getJSONObject("data");
	    } else {
		log.error("OperationPlatformServiceImpl.queryCustomerInfo 出错,错误码:" + flag + ";错误信息:" + desc);
		return null;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.queryCustomerInfo", e);
	    return null;
	}
    }

    /**
     * 6.5　授权认证接口upd 用于处理用户信息-在运营平台注册
     * 
     * @param peripheryCode
     *            系统对接码
     * @param custAccount
     *            账号custAccount标识
     * @param userIdentify
     *            用户标识
     * @return
     */
    @Override
    public boolean manageUserByOP(String peripheryCode, String custAccount, String userIdentify) {
	log.info("OperationPlatformServiceImpl.manageUserByOP 用户信息注册接口");
	// 用户后面返回是否成功标识
	boolean result = false;
	try {
	    // 判断账号信息是否正确
	    if (StringUtils.isNotBlank(custAccount) && StringUtils.isNotBlank(peripheryCode) && StringUtils.isNotBlank(userIdentify)) {
		// 获取运营平台注册账号地址
		String Myurl = url + "/CombinationServer/oauth";
		;
		try {
		    // 拼写相应参数
		    JSONObject jsonParam = new JSONObject();
		    jsonParam.put("periphery_code", peripheryCode);
		    jsonParam.put("periphery_account", custAccount);
		    jsonParam.put("periphery_user_account", userIdentify);
		    // 获取返回值
		    String data = HttpRequest.postData(Myurl, jsonParam.toString());
		    log.info("OperationPlatformServiceImpl.manageUserByOP url:" + Myurl + "---request:" + jsonParam.toString());
		    log.info("OperationPlatformServiceImpl.manageUserByOP response:" + data);
		    JSONObject resultData = JSONObject.fromObject(data);
		    // 获取判断注册是否成功
		    String flag = resultData.getString("flag");
		    if ("0000".equals(flag)) {
			result = true;
		    }
		} catch (Exception ex) {
		    ex.getMessage();
		    return result;
		}
	    } else {
		result = true;
	    }
	} catch (Exception e) {
	    log.error("OperationPlatformServiceImpl.manageUserByOP 运营平台存储用户失败" + e);
	    result = false;
	}
	return result;
    }

    /**
     * 6.5　授权认证接口 3 账号注册接口（不合并） 用户选择不进行合并账号，想生成一个新的账号。
     * 
     * @param object
     *            返回值
     * @param appKey
     *            应用标识
     * @param peripheryCode
     *            系统接入码
     * @param companyIdentify
     *            企业标识
     * @param relevanceCode
     *            账号关联码
     * @param relevancePhone
     *            手机号码
     * @param channelAccount
     *            渠道码
     * @param companyName
     *            企业名称
     * @param linker
     *            联系人姓名
     * @param email
     *            邮箱
     * @return
     */
    public String accountRegiste(JSONObject object, String appKey, String peripheryCode, String companyIdentify, String relevanceCode, String relevancePhone,
	    String channelAccount, String companyName, String linker, String email) {
	log.info("OperationPlatformServiceImpl.accountRegiste 运营平台账号注册");
	JSONObject resultData = new JSONObject();
	String custAccount = null;
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("periphery_code", peripheryCode);// 外围系统编码
	    jsonParam.put("cust_type", "cpc");// 注册客户类型cpc:企业客户(默认)idc:个人客户
	    jsonParam.put("periphery_account", companyIdentify);// 外围系统账号
	    jsonParam.put("email", email);// 管理员账号邮箱
	    jsonParam.put("mobilephone", relevancePhone);// 管理员手机号
	    jsonParam.put("is_send_email", "1");
	    jsonParam.put("app_key", appKey);
	    jsonParam.put("channel_account", channelAccount);
	    jsonParam.put("cust_name", companyName);
	    jsonParam.put("cust_contact", linker);
	    jsonParam.put("relatedCode", relevanceCode);
	    jsonParam.put("mobile", relevancePhone);

	    // 调用运营平台
	    String myurl = url + "/CombinationServer/oauth";
	    String data = HttpRequest.postData(myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.accountRegiste url:" + myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.accountRegiste response:" + data);

	    resultData = JSONObject.fromObject(data);
	    // 获取是否注册成功
	    String flag = resultData.getString("flag");
	    // 判断是否注册成功
	    if ("0000".equals(flag)) {
		custAccount = JSONObject.fromObject(resultData.get("data")).getString("cust_account");
		object.put("result", "0");
		object.put("describe", "运营平台注册成功");
	    } else {
		object.put("result", "-1");
		object.put("describe", "错误:" + resultData.getString("desc"));
	    }
	} catch (Exception ex) {
	    ex.printStackTrace();
	    log.error("OperationPlatformServiceImpl.accountRegiste 运营平台账号注册失败" + ex);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + ex);
	}
	return custAccount;
    }

    /**
     * 6.13　客户信息修改接口 6、账号更新接口（不合并，普通的更新接口）---与3对应 信息账号的基本信息。 返回：账号基本信息
     * 
     * @param cust_account
     *            客户账号
     * @param email
     *            //管理员账号邮箱
     * @param relevancePhone
     *            //管理员手机号
     * @param channelAccount
     *            //渠道码
     * @param companyName
     *            //企业名称
     * @return
     */
    public JSONObject UpdateCustomerInfo(String cust_account, String email, String relevancePhone, String channelAccount, String companyName) {
	log.info("OperationPlatformServiceImpl.UpdateCustomerInfo 客户信息修改接口");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("cust_account", cust_account);// 客户账号
	    jsonParam.put("email", email);// 管理员账号邮箱
	    jsonParam.put("mobilephone", relevancePhone);// 管理员手机号
	    jsonParam.put("channel_account", channelAccount);// 渠道码
	    jsonParam.put("cust_name", companyName);// 企业名称

	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/updateCustomerInfo.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.UpdateCustomerInfo url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.UpdateCustomerInfo response:" + data);

	    object = JSONObject.fromObject(data);
	    String flag = object.getString("flag");
	    if ("0000".equals(flag)) {
		object.put("result", "0");
		object.put("describe", "OK");
		object.remove("flag");
		object.remove("desc");
	    } else {
		object.put("result", "-1");
		object.put("describe", object.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.UpdateCustomerInfo 客户信息修改接口" + e);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 6.30 可用余额资源汇总接口
     * 
     * @param app_key
     * @param cust_account
     * @return
     */
    public JSONObject queryResourceAccountSum(String app_key, String cust_account) {
	log.info("OperationPlatformServiceImpl.queryResourceAccountSum 可用余额资源汇总接口");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("cust_account", cust_account);// 客户账号
	    jsonParam.put("app_key", app_key);// 
	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/queryResourceAccountSum.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.queryResourceAccountSum url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.queryResourceAccountSum response:" + data);
	    object = JSONObject.fromObject(data);
	    String flag = object.getString("flag");
	    if ("0000".equals(flag)) {
		object.put("result", "0");
		object.put("describe", "OK");
		object.remove("flag");
		object.remove("desc");
	    } else {
		object.put("result", "-1");
		object.put("describe", object.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.queryResourceAccountSum 可用余额资源汇总接口" + e);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 6.35　查询ISV分页接口 查询ISV 分页
     * 
     * @param isv_id
     * @param isv_name
     * @param page_no
     *            页码
     * @param page_size
     *            每页显示数据数量
     * @return
     */
    public List<ParamBean> queryIsvItems(String isv_id, String isv_name, Integer page_no, Integer page_size) {
	List<ParamBean> resultList = new ArrayList<ParamBean>();
	JSONObject object = new JSONObject();
	if (EnumUtility.isBlank(page_no)) {
	    object.put("page_no", 1);
	} else {
	    object.put("page_no", page_no);
	}
	if (EnumUtility.isBlank(page_size)) {
	    object.put("page_size", 10);
	} else {
	    object.put("page_size", page_size);
	}

	if (EnumUtility.isNotBlank(isv_id)) {
	    object.put("isv_id", isv_id);
	}
	JSONObject response = new JSONObject();
	try {
	    if (EnumUtility.isNotBlank(isv_name)) {
		object.put("isv_name", java.net.URLEncoder.encode(isv_name, "utf-8"));
	    }
	    String myUrl = url + "/pubbase/api/isv/queryIsvItems.do";
	    String data = HttpRequest.postData(myUrl, object.toString());
	    log.info("OperationPlatformServiceImpl.queryIsvItems url=" + myUrl);
	    log.info("OperationPlatformServiceImpl.queryIsvItems req=" + object.toString());
	    log.info("OperationPlatformServiceImpl.queryIsvItems respone=" + data);
	    response = JSONObject.fromObject(data);
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.queryIsvItems", e);
	    return resultList;
	}
	String flag = response.getString("flag");
	String desc = response.getString("desc");
	if (flag.equals("0000")) {
	    JSONArray dataArray = response.getJSONArray("data");
	    for (int i = 0; i < dataArray.size(); i++) {
		JSONObject json = dataArray.getJSONObject(i);
		ParamBean bean = new ParamBean();
		bean.setId(json.getString("isv_id"));
		bean.setText(json.getString("isv_name"));
		resultList.add(bean);
	    }
	} else {
	    log.error("OperationPlatformServiceImpl.queryIsvItems 出错,错误码:" + flag + ";错误信息:" + desc);
	}
	return resultList;
    }

    /**
     * 6.36　查询APP分页接口 查询app 分页基础接口
     * 
     * @param isvName
     * @param app_name
     * @param app_key
     * @param page_no
     *            页码
     * @param page_size
     *            每页显示数据数量
     * @return
     */
    public JSONObject queryAppItems(String isvName, String app_name, String app_key, Integer page_no, Integer page_size) {
	JSONObject object = new JSONObject();
	if (EnumUtility.isBlank(page_no)) {// 当前页
	    object.put("page_no", 1);
	} else {
	    object.put("page_no", page_no);
	}
	if (EnumUtility.isBlank(page_size)) {// 显示个数
	    object.put("page_size", 10);
	} else {
	    object.put("page_size", page_size);
	}
	try {
	    if (EnumUtility.isNotBlank(isvName)) {// ISV名称
		object.put("isv_name", java.net.URLEncoder.encode(isvName, "utf-8"));
	    }
	    if (EnumUtility.isNotBlank(app_name)) {// APP名称
		object.put("app_name", java.net.URLEncoder.encode(app_name, "utf-8"));
	    }
	    if (EnumUtility.isNotBlank(app_key)) {// app_key
		object.put("app_key", app_key);
	    }
	    String Myurl = url + "/pubbase/api/app/queryAppItems.do";
	    String data = HttpRequest.postData(Myurl, object.toString());
	    log.info("OperationPlatformServiceImpl.queryAppItems url=" + Myurl);
	    log.info("OperationPlatformServiceImpl.queryAppItems req=" + object.toString());
	    log.info("OperationPlatformServiceImpl.queryAppItems1 respone=" + data);

	    /* 解释返回信息 */
	    JSONObject response = JSONObject.fromObject(data);
	    String flag = response.getString("flag");
	    String desc = response.getString("desc");
	    if (flag.equals("0000")) {
		return response;
	    } else {
		log.error("OperationPlatformServiceImpl.queryAppItems 出错,错误码:" + flag + ";错误信息:" + desc);
		return null;
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.queryAppItems", e);
	    return null;
	}
    }

    /**
     * 6.37　验证产品余额接口 运营平台验证账号是否可用
     * 
     * @param object
     * @param appExtendBean
     * @param accountBean
     * @param abilityType
     * @return
     */
    @Override
    public boolean platformAuthentication(JSONObject object, AppExtendBean appExtendBean, AccountBean accountBean, String abilityType) {
	log.info("OperationPlatformServiceImpl.platformAuthentication 运营平台验证账号是否可用");
	// 后面的返回值
	boolean isTrue = false;
	// 获取运营账号标识
	String custAccount = accountBean.getCust_account();
	// 获取运营平台鉴权地址
	String url = accountBean.getUrl_authentication();
	// 获取应用key
	String appKey = appExtendBean.getApp_key();
	// 判断获取的值是否为空
	if (StringUtils.isNotBlank(custAccount) && StringUtils.isNotBlank(url) && StringUtils.isNotBlank(appKey)) {
	    // 传入参数
	    String product_type = "yuy";
	    if (abilityType.equals(AbilityTypeEnum.SMMS.getKey())) {
		product_type = "sms";
	    }
	    JSONObject jsonP = new JSONObject();
	    jsonP.put("cust_account", custAccount);
	    jsonP.put("app_key", appKey);
	    jsonP.put("product_key", abilityType);
	    jsonP.put("product_type", product_type);

	    // 获取查询值
	    String retP = HttpRequest.postData(url, jsonP.toString());
	    log.info("验证账号----req:" + jsonP.toString() + "-----response:" + retP);
	    if (StringUtils.isBlank(retP)) {
		object.put("result", "-1");
		object.put("describe", "鉴权失败");
		object.put("code", "0003");
		object.put("errorMsg", "运营平台返回为空");
		return isTrue;
	    }
	    // 解析返回值
	    JSONObject jsonResultP = JSONObject.fromObject(retP);

	    if (!JSONUtils.isNull(jsonResultP)) {
		// 最后返回的值
		String flag = (String) jsonResultP.get("flag");
		// 返回结果说明
		String desc = (String) jsonResultP.get("desc");
		// 如果运营平台返回‘0000’，则说明可以发起通话
		if (flag.equals("0000")) {
		    isTrue = true;
		    object.put("result", "0");
		    object.put("describe", "OK");
		} else if (flag.equals("0042")) {
		    isTrue = false;
		    object.put("result", "-1");
		    object.put("errorMsg", desc);
		    object.put("describe", "计费账号余额不足");
		    object.put("code", "9999");
		} else {

		    isTrue = false;
		    object.put("result", "-1");
		    object.put("errorMsg", desc);
		    object.put("describe", "鉴权失败");
		    object.put("code", "0003");
		}
	    } else {
		isTrue = false;
		object.put("result", "-1");
		object.put("errorMsg", "运营平台返回值:" + retP + ";解释失败");
		object.put("describe", "鉴权失败");
		object.put("code", "0003");
	    }
	} else {
	    isTrue = false;
	    object.put("result", "-1");
	    object.put("errorMsg", "参数传递不全:appKey=" + appKey + "&custAccount=" + custAccount + "&url=" + url);
	    object.put("describe", "鉴权失败");
	    object.put("code", "0003");
	}

	return isTrue;
    }

    /**
     * 6.38　由关联关系查询计费账号信息列表接口 1 账号信息列表接口 H5页面在注册前，H5页面输入“关联码”、关联手机号等然后调用此接口。
     * 返回与关联码一致的所有账号，让用户进行选择。
     * 
     * @param periphery_code
     *            外围系统编码
     * @param mobile
     *            手机号码
     * @param relatedCode
     *            嘟嘟关联账号
     * @return
     */
    public JSONObject queryCustAccountByRelateInfo(String periphery_code, String mobile, String relatedCode) {
	log.info("OperationPlatformServiceImpl.queryCustAccountByRelateInfo 由关联关系查询计费账号信息列表接口");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("periphery_code", periphery_code);// 外围系统编码
	    jsonParam.put("mobile", mobile);// 手机号码
	    jsonParam.put("relatedCode", relatedCode);// 嘟嘟关联账号
	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/queryCustAccountByRelateInfo.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.queryCustAccountByRelateInfo url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.queryCustAccountByRelateInfo response:" + data);
	    object = JSONObject.fromObject(data);
	    String flag = object.getString("flag");
	    // 判断是否注册成功
	    if ("0000".equals(flag)) {
		JSONArray dataArray = object.getJSONArray("data");
		if (dataArray.size() > 0) {
		    object.put("status", "0001");
		} else {
		    object.put("status", "0000");
		}
		object.put("result", "0");
		object.put("describe", "OK");
		object.remove("flag");
		object.remove("desc");
		return object;
	    } else {
		object.put("result", "-1");
		object.put("describe", object.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.queryCustAccountByRelateInfo 由关联关系查询计费账号信息列表接口" + e);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 6.40　合并计费账户接口
     * 
     * @param cust_account
     *            主账号信息
     * @param accounts
     *            被合并的账号信息
     * @return
     */
    public JSONObject CombineCustAccount(String cust_account, JSONArray accounts) {
	log.info("OperationPlatformServiceImpl.CombineCustAccount 合并计费账户接口");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("cust_account", cust_account);// 客户账号
	    jsonParam.put("accounts", accounts);
	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/combineCustAccount.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.updQueryCustAccountByRelateInfo url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.updQueryCustAccountByRelateInfo response:" + data);

	    JSONObject result = JSONObject.fromObject(data);
	    String flag = result.getString("flag");
	    if ("0000".equals(flag)) {
		object.put("dataJSON", result.getJSONArray("data"));
		object.put("result", "0");
		object.put("describe", "OK");
	    } else {
		object.put("result", "-1");
		object.put("describe", result.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.CombineCustAccount 合并计费账户接口" + e);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 6.41　查询企业所在的应用列表接口 查询账号(企业 )信息接口
     * 
     * @param page_no
     *            当前页
     * @param pageSize
     *            一页显示多少条
     * @param isv_name
     *            ISV名称
     * @param app_name
     *            应用名称
     * @param cust_account
     *            账号标识
     * @return
     */
    public JSONObject queryItems(Integer page_no, Integer pageSize, String isv_name, String app_name, String cust_account) {
	JSONObject object = new JSONObject();
	if (EnumUtility.isBlank(page_no)) {
	    object.put("page_no", 1);
	} else {
	    object.put("page_no", page_no);
	}
	if (EnumUtility.isBlank(pageSize)) {
	    object.put("page_size", 10);
	} else {
	    object.put("page_size", pageSize);
	}
	if (EnumUtility.isNotBlank(cust_account)) {
	    object.put("cust_account", cust_account.trim());
	}
	try {
	    if (EnumUtility.isNotBlank(isv_name)) {
		object.put("isv_name", java.net.URLEncoder.encode(isv_name.trim(), "utf-8"));
	    }
	    if (EnumUtility.isNotBlank(app_name)) {
		object.put("app_name", java.net.URLEncoder.encode(app_name.trim(), "utf-8"));
	    }
	    String Myurl = url + "/pubbase/api/cust/queryCustAppList.do";
	    String data = HttpRequest.postData(Myurl, object.toString());
	    log.info("OperationPlatformServiceImpl.queryItems url=" + Myurl);
	    log.info("OperationPlatformServiceImpl.queryItems req=" + object.toString());
	    log.info("OperationPlatformServiceImpl.queryItems respone=" + data);
	    JSONObject response = JSONObject.fromObject(data);
	    String flag = response.getString("flag");
	    String desc = response.getString("desc");

	    if (flag.equals("0000")) {
		return response;
	    } else {
		log.error("OperationPlatformServiceImpl.queryItems 出错,错误码:" + flag + ";错误信息:" + desc);
		return null;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.queryItems", e);
	    return null;
	}
    }

    /**
     * 6.42　授权账号注册（强制合并T）接口 4、账号注册（强制合并）接口 客户调用此接口，只要是关联码一致的账号全部合成一个新的账号。
     * 返回：主账号信息，副账号信息（被合并的账号）
     * 
     * @param periphery_code
     *            外系统编码
     * @param company_identify
     *            企业（个人）标识
     * @param email
     *            邮箱
     * @param relevancePhone
     *            关系手机号
     * @param app_key
     *            应用标识
     * @param channelAccount
     *            渠道码
     * @param companyName
     *            企业名称
     * @param linker
     *            联系人
     * @param relatedCode
     *            关联码
     * @return
     */
    public JSONObject OauthCombineForceInterface(String periphery_code, String company_identify, String email, String relevancePhone, String app_key, String channelAccount,
	    String companyName, String linker, String relatedCode) {
	log.info("OperationPlatformServiceImpl.OauthCombineForceInterface 授权账号注册（强制合并T）接口");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("periphery_code", periphery_code);// 外围系统编码
	    jsonParam.put("cust_type", "cpc");// 注册客户类型cpc:企业客户(默认)idc:个人客户
	    jsonParam.put("periphery_account", company_identify);// 外围系统账号
	    jsonParam.put("email", email);// 管理员账号邮箱
	    jsonParam.put("mobilephone", relevancePhone);// 管理员手机号
	    jsonParam.put("app_key", app_key);// 应用key
	    jsonParam.put("is_send_email", "1");// 是否邮件发送注册信息
	    jsonParam.put("channel_account", channelAccount);// 渠道码
	    jsonParam.put("cust_name", companyName);// 企业名称
	    jsonParam.put("cust_contact", linker);// 企业客户联系人
	    jsonParam.put("relatedCode", relatedCode);// 嘟嘟关联账号

	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/oauthCombineForce.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.OauthCombineForceInterface url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.OauthCombineForceInterface response:" + data);

	    JSONObject result = JSONObject.fromObject(data);
	    String flag = result.getString("flag");
	    if (flag.equals("0000")) {
		object.put("result", "0");
		object.put("describe", "OK");
		object.put("data", result.getJSONArray("data"));
	    } else {
		object.put("result", "-1");
		object.put("describe", result.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.OauthCombineForceInterface 授权账号注册（强制合并T）接口" + e);
	    object.put("result", "-1");
	    object.put("code", "0100");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 6.43　客户信息修改（强制合并T）接口 7、账号更新（强制合并）接口--与4对应
     * 客戾调用更新接口时输入关联码，强制把关联码一致的账号全部合成一个新的账号。 返回：主账号信息，副账号信息（被合并的账号）
     * 
     * @param appKey
     * @param cust_account
     *            客户账号
     * @param email
     *            //管理员账号邮箱
     * @param relevancePhone
     *            //管理员手机号
     * @param channelAccount
     *            //渠道码
     * @param companyName
     *            //企业名称
     * @param relatedCode
     *            //嘟嘟关联账号
     * @return
     */
    public JSONObject UpdateCustomerForceInfo(String appKey, String cust_account, String email, String relevancePhone, String channelAccount, String companyName, String relatedCode) {
	log.info("OperationPlatformServiceImpl.UpdateCustomerForceInfo 客户信息修改（强制合并T）接口");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("app_key", appKey);
	    jsonParam.put("cust_account", cust_account);// 客户账号
	    jsonParam.put("email", email);// 管理员账号邮箱
	    jsonParam.put("mobilephone", relevancePhone);// 管理员手机号
	    jsonParam.put("channel_account", channelAccount);// 渠道码
	    jsonParam.put("cust_name", companyName);// 企业名称
	    jsonParam.put("relatedCode", relatedCode);// 嘟嘟关联账号
	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/updateCustomerForceInfo.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.UpdateCustomerForceInfo url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.UpdateCustomerForceInfo response:" + data);

	    object = JSONObject.fromObject(data);
	    String flag = object.getString("flag");
	    // 判断是否注册成功
	    if ("0000".equals(flag)) {
		object.put("result", "0");
		object.put("describe", "OK");
		object.remove("flag");
		object.remove("desc");
		return object;
	    } else {
		object.put("result", "-1");
		object.put("describe", object.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.UpdateCustomerForceInfo 客户信息修改（强制合并T）接口" + e);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 6.44　授权账号注册（合并）接口 2、账号合并接口 用户选择完主账号，副账号（被合并的账号）调用此接口进行合并操作。
     * 
     * @param periphery_code
     *            外围系统编码
     * @param app_key
     *            应用标识
     * @param company_identify
     * @param channelAccount
     *            渠道码
     * @param relatedCode
     *            账号关联码
     * @param cust_account
     *            主账号
     * @param accounts
     *            被合并账号此为JSONArray(combine_cust_account)
     * @return
     */
    public JSONObject OauthCombineInterface(String periphery_code, String app_key, String company_identify, String channelAccount, String relatedCode, String cust_account,
	    JSONArray accounts) {
	log.info("OperationPlatformServiceImpl.OauthCombineInterface 授权账号注册（合并）接口");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("periphery_code", periphery_code);// 外围系统编码
	    jsonParam.put("cust_type", "cpc");// 注册客户类型cpc:企业客户(默认)idc:个人客户
	    jsonParam.put("periphery_account", company_identify);// 外围系统账号
	    jsonParam.put("app_key", app_key);// 应用key
	    jsonParam.put("channel_account", channelAccount);// 渠道账号
	    jsonParam.put("relatedCode", relatedCode);// 嘟嘟关联账号
	    jsonParam.put("cust_account", cust_account);// 运营平台客户账号
	    jsonParam.put("accounts", accounts);// 账户数组
	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/oauthCombine.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.OauthCombineInterface url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.OauthCombineInterface response:" + data);

	    JSONObject result = JSONObject.fromObject(data);
	    String flag = result.getString("flag");
	    if (flag.equals("0000")) {
		object.put("result", "0");
		object.put("describe", "OK");
		object.put("data", result.getJSONObject("data"));
	    } else {
		object.put("result", "-1");
		object.put("describe", result.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.OauthCombineInterface 授权账号注册（合并）接口" + e);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 6.45　客户信息修改接口（不合并，只做更新关联码与手机号） 5、账号更新接口---与1对应。
     * H5页面在做账号更新前，H5页面输入“关联码”、关联手机号等然后调用此接口。 返回与关联码一致的所有账号，让用户进行选择。
     * 
     * @param cust_account
     *            客户账号
     * @param email
     *            //管理员账号邮箱
     * @param relevancePhone
     *            //管理员手机号
     * @param channelAccount
     *            //渠道码
     * @param companyName
     *            //企业名称
     * @param relatedCode
     *            //嘟嘟关联账号
     * @return
     */
    public JSONObject UpdateCustomerCombineInfos(String cust_account, String email, String relevancePhone, String channelAccount, String companyName, String relatedCode) {
	log.info("OperationPlatformServiceImpl.UpdateCustomerCombineInfo 客户信息修改（合并）接口");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("cust_account", cust_account);// 客户账号
	    jsonParam.put("email", email);// 管理员账号邮箱
	    jsonParam.put("mobilephone", relevancePhone);// 管理员手机号
	    jsonParam.put("channel_account", channelAccount);// 渠道码
	    jsonParam.put("cust_name", companyName);// 企业名称
	    jsonParam.put("relatedCode", relatedCode);// 嘟嘟关联账号
	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/updateCustomerCombineInfo.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.UpdateCustomerCombineInfo url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.UpdateCustomerCombineInfo response:" + data);

	    object = JSONObject.fromObject(data);
	    String flag = object.getString("flag");
	    // 判断是否注册成功
	    if ("0000".equals(flag)) {
		object.put("result", "0");
		object.put("describe", "OK");
		object.remove("flag");
		object.remove("desc");
		return object;
	    } else {
		object.put("result", "-1");
		object.put("describe", object.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.UpdateCustomerCombineInfo 客户信息修改（合并）接口" + e);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 6.46　订购应用下所有产品接口 运营平台订购相关
     * 
     * @param object
     *            返回值
     * @param appKey
     *            应用标识
     * @param custAccount
     *            账号编码
     * @return
     */
    @Override
    public boolean platformOrder(JSONObject object, String appKey, String custAccount) {
	log.info("OperationPlatformServiceImpl.platformOrder 运营平台订购");
	// 后面的返回值
	boolean isTrue = false;
	try {
	    // 拼写相应参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("app_key", appKey);
	    jsonParam.put("cust_account", custAccount);
	    // 访问运营平台
	    // 获取建立订购关系的运营平台配置地址
	    String myurl = url + "/CombinationServer/subscribeProduct";
	    String data = HttpRequest.postData(myurl, jsonParam.toString());
	    JSONObject resultData = JSONObject.fromObject(data);
	    log.info("OperationPlatformServiceImpl.platformOrder response:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.platformOrder response:" + data);

	    // 获取是否订购成功
	    String flag = resultData.getString("flag");
	    // 判断是否订购成功
	    if ("0000".equals(flag)) {
		isTrue = true;
		object.put("result", "0");
		object.put("describe", "订购关系创建成功:" + data);
	    } else {
		isTrue = false;
		object.put("result", "-1");
		object.put("describe", "运营平台订购关系创建异常:" + resultData.getString("desc"));
	    }
	} catch (Exception ex) {
	    isTrue = false;
	    object.put("result", "-1");
	    object.put("describe", "订购关系创建异常:" + ex);
	    log.error("OperationPlatformServiceImpl.platformOrder 运营平台订购失败：" + ex);
	}
	return isTrue;
    }

    /**
     * 6.47　更新关联关系并查询计费账号信息列表接口 更新关联码,并返回账号列表
     * 
     * @param cust_account
     * @param app_key
     * @param mobilePhone
     * @param channel_account
     * @param relatedCode
     * @param periphery_code
     * @return
     */
    public JSONObject updQueryCustAccountByRelateInfo(String cust_account, String app_key, String mobilePhone, String channel_account, String relatedCode, String periphery_code,
	    String email) {
	log.info("OperationPlatformServiceImpl.updQueryCustAccountByRelateInfo 更新关联码,并返回账号列表");
	JSONObject object = new JSONObject();
	try {
	    // 拼写相应传递参数
	    JSONObject jsonParam = new JSONObject();
	    jsonParam.put("app_key", app_key);
	    jsonParam.put("cust_account", cust_account);// 客户账号
	    jsonParam.put("mobilephone", mobilePhone);// 管理员手机号
	    jsonParam.put("channel_account", channel_account);// 渠道码
	    jsonParam.put("relatedCode", relatedCode);// 嘟嘟关联账号
	    jsonParam.put("periphery_code", periphery_code);// 外围系统编码
	    jsonParam.put("email", email);
	    // 调用运营平台
	    String Myurl = url + "/CombinationServer/updQueryCustAccountByRelateInfo.do";
	    String data = HttpRequest.postData(Myurl, jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.updQueryCustAccountByRelateInfo url:" + Myurl + "---request:" + jsonParam.toString());
	    log.info("OperationPlatformServiceImpl.updQueryCustAccountByRelateInfo response:" + data);

	    object = JSONObject.fromObject(data);
	    String flag = object.getString("flag");
	    // 判断是否注册成功
	    if ("0000".equals(flag)) {
		JSONArray dataArray = object.getJSONArray("data");
		if (dataArray.size() > 0) {
		    object.put("status", "0001");
		} else {
		    object.put("status", "0000");
		}
		object.put("result", "0");
		object.put("describe", "OK");
		object.remove("flag");
		object.remove("desc");
		return object;
	    } else {
		object.put("result", "-1");
		object.put("describe", object.getString("desc"));
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.updQueryCustAccountByRelateInfo 更新关联码,并返回账号列表" + e);
	    object.put("result", "-1");
	    object.put("describe", "错误:" + e);
	}
	return object;
    }

    /**
     * 1.51　统计注册数目接口 统计日新增数
     * 
     * @param oauth_date
     * @return
     */
    public List<JSONObject> queryRegisterNum(String oauth_date) {
	List<JSONObject> dataList = new ArrayList<JSONObject>();
	try {
	    JSONObject object = new JSONObject();
	    object.put("oauth_date", oauth_date);
	    String Myurl = url + "/CombinationServer/queryRegisterNum.do";
	    String data = HttpRequest.postData(Myurl, object.toString());
	    log.info("OperationPlatformServiceImpl.queryRegisterNum url=" + Myurl);
	    log.info("OperationPlatformServiceImpl.queryRegisterNum req=" + object.toString());
	    log.info("OperationPlatformServiceImpl.queryRegisterNum respone=" + data);
	    JSONObject response = JSONObject.fromObject(data);
	    String flag = response.getString("flag");
	    String desc = response.getString("desc");
	    if (flag.equals("0000")) {
		dataList = response.getJSONArray("data");
	    } else {
		log.error("OperationPlatformServiceImpl.queryRegisterNum 出错,错误码:" + flag + ";错误信息:" + desc);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    log.error("OperationPlatformServiceImpl.queryRegisterNum", e);
	}

	return dataList;
    }

    public static void main(String[] args) {
	String relatedCode = "20160523gaoxs";// 嘟嘟关联账号

	String relevancePhone = "17051400333";// 手机号码
	String companyIdentify = "17051400333";// 企业标识

	String app_key = "00F57953494DB1429E31332C4BAD5AB5";// 应用标识
	String periphery_code = "H5DEMO";// 外围系统编码

	String channelAccount = "";// 渠道码
	// String load_cust_account="1d9b348e-4921-4d30-b4da-f214935a644a";//主账号
	String vice_cust_account = "17001400333";// 被合并账号

	String companyName = "企业名称";// 企业名称
	String linker = "联系人";// 联系人姓名
	String email = "gaoxs@139.com";// 邮箱
	String cust_account = "1d9b348e-4921-4d30-b4da-f214935a644a";// 修改者的账号
	OperationPlatformServiceImpl o = new OperationPlatformServiceImpl();

	// 6.38　由关联关系查询计费账号信息列表接口测试完成OK
	// Object
	// result=o.queryCustAccountByRelateInfo(periphery_code,relevancePhone,relatedCode);
	// 6.44　授权账号注册（合并）接口 （OK）
	JSONArray accounts = new JSONArray();// 被合并账号
	JSONObject object = new JSONObject();
	object.put("combine_cust_account", vice_cust_account);
	accounts.add(object);
	// o.OauthCombineInterface(periphery_code,app_key,companyIdentify,channelAccount,relatedCode,load_cust_account,accounts
	// );

	// 6.5　授权认证接口(OK)
	// o.accountRegiste(object, app_key, periphery_code,companyIdentify,
	// relatedCode,relevancePhone,channelAccount, companyName,linker,email);
	// o.platformOrder(object, app_key,
	// "1d9b348e-4921-4d30-b4da-f214935a644a");
	// 6.42　授权账号注册（强制合并T）接口 有问题（无测试数据）
	// o.OauthCombineForceInterface(periphery_code,companyIdentify,email,relevancePhone,app_key,channelAccount,companyName,linker,relatedCode);

	// 6.45　客户信息修改（合并）接口 有问题（其他异常）
	// o.UpdateCustomerCombineInfos(cust_account,email,relevancePhone,channelAccount,companyName,relatedCode);

	// 6.13　客户信息修改接口测试完成（OK）
	// o.UpdateCustomerInfo(cust_account,email,relevancePhone,channelAccount,companyName);

	// 6.43　客户信息修改（强制合并T）接口 有问题（无测试数据）
	// o.UpdateCustomerForceInfo(app_key,cust_account,email,relevancePhone,channelAccount,companyName,relatedCode);
    }
}
